<?php
//RabbitMQ 测试demo - 发布者

//配置信息 
$conn_args = array( 
    'host' => '127.0.0.1',  
    'port' => '5672',  
    'login' => 'guest',  
    'password' => 'guest', 
    'vhost'=>'/' 
);   
$e_name = 'e_linvo'; //交换机名 
// $q_name = 'q_linvo1'; //无需队列名 生产者队列不需要设置队列 会自动丢队列里
$k_route = 'key_1'; //路由key 
 
//创建连接和channel 
$conn = new AMQPConnection($conn_args);   
if (!$conn->connect()) {   
    die("Cannot connect to the broker!\n");   
}   
$channel = new AMQPChannel($conn);   
 
//创建交换机对象    
$ex = new AMQPExchange($channel);   
$ex->setName($e_name);   
date_default_timezone_set("Asia/Shanghai");

$startime=microtime(true);
//发送消息 
// $channel->startTransaction(); //开始事务  
$count = 20;
for($i=0; $i<$count; ++$i){
    // usleep(100000);//休眠1秒
    //消息内容 
    $milliseconds = round((microtime(true) - floor(microtime(true))) * 10000);
    $message = "test msg! ".date("H:i:s").' '.$milliseconds;   
    $ex->publish($message, $k_route);
    // echo "Send Message:".$ex->publish($message, $k_route).PHP_EOL;  
} 
$endtime = microtime(true);
$totaltime   = $endtime-$startime;
$timecost 	 = round($totaltime*1000);
echo $count.'条消息发送完毕'.PHP_EOL.'运行时间:' .$timecost.' ms';
// $channel->commitTransaction(); //提交事务 
 
$conn->disconnect();
?>